package com.chuangke.sinotrukbudget.data.builder;

import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.druid.util.StringUtils;
import com.chuangke.common.SpringContextUtil;
import com.chuangke.core.data.builder.DataBuildConfig;
import com.chuangke.core.data.builder.IDataBuilder;
import com.chuangke.sinotrukbudget.model.BudgetAccount;
import com.chuangke.sinotrukbudget.service.BudgetAccountService;

public class BudgetAccountBuilder implements IDataBuilder<BudgetAccount> {

	private static Logger logger = LoggerFactory.getLogger(BudgetAccountBuilder.class);

	public static final String NAME = "budget-account-builder";

	@Override
	public String getName() {
		return NAME;
	}

	@Override
	public Map<String, BudgetAccount> getMetaData() {
		BudgetAccountService sysDeptService = SpringContextUtil.getBean(BudgetAccountService.class);
		return sysDeptService.findMap();
	}

	@Override
	public void build(Object obj, Map<String, BudgetAccount> metaData, List<DataBuildConfig> dataBuildConfigList) {
		if(metaData == null) return ;
		try {
			for (DataBuildConfig dataBuildBean : dataBuildConfigList) {
				String id = BeanUtils.getProperty(obj, dataBuildBean.getIdColumn());
				if(StrUtil.isBlank(id)) {
					continue ;
				}
				BudgetAccount dept = metaData.get(id);
				String value = dept == null ? "" : dept.getName();
				BeanUtils.setProperty(obj, dataBuildBean.getNameColumn(), value);
			}
		} catch (Exception e) {
			logger.error("设置字段信息出错", e);
		}
	}

}